//
//  AddFriendViewController.m
//  SouthChina
//
//  Created by Mr.Y on 14-10-9.
//  Copyright (c) 2014年 科创. All rights reserved.
//

#import "AddFriendViewController.h"
#import "FriendDetailViewController.h"
#import "SearchinforTableViewCell.h"
#import "FaceAddFriendViewController.h"
@interface AddFriendViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
{
    NSArray * listArr;
}
@property (strong, nonatomic) UITableView *searchInforTableview;
@property (strong, nonatomic) UISearchBar * searchBar;

@end

@implementation AddFriendViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

       

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self addTitleBarWithBackBtn:@"添加好友" needReturn:YES];
    //搜索框
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, TITLE_BAR_HEGIHT+(YNEW), SCREEN_WIDTH, 44)];
    _searchBar.showsCancelButton=NO;
    _searchBar.delegate = self;
    _searchBar.barStyle=UIBarStyleDefault;
    _searchBar.placeholder = @"学工号/姓名";//1012
    [self.view addSubview:_searchBar];
    
    //添加好友按钮
    //UIImage *img=[UIImage imageNamed:@"blue_btn_normal.png"];
    //img=[img stretchableImageWithLeftCapWidth:20 topCapHeight:20];
    UIButton * addFriendBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, TITLE_BAR_HEGIHT+(YNEW) + _searchBar.frame.size.height + 10, SCREEN_WIDTH-10, 35)];
    [addFriendBtn addTarget:self action:@selector(addfriendbtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addFriendBtn];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(5,2, 30, 30)];
    imgView.image = [UIImage imageNamed:@"face2face.png"];
    imgView.layer.masksToBounds = YES;
    imgView.layer.cornerRadius = 15;
    [addFriendBtn addSubview:imgView];
    
    UILabel *btLabel = [[UILabel alloc] initWithFrame:CGRectMake(45,2, 120, 30)];
    btLabel.text = @"面对面加好友";
    btLabel.textAlignment = NSTextAlignmentLeft;
    [addFriendBtn addSubview:btLabel];
    
    //搜索好友列表
    _searchInforTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, addFriendBtn.frame.origin.y+40, SCREEN_WIDTH, SCREEN_HEIGHT-addFriendBtn.frame.origin.y-40) style:UITableViewStylePlain];
    _searchInforTableview.delegate = self;
    _searchInforTableview.dataSource = self;
    _searchInforTableview.bounces = YES;
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [_searchInforTableview setTableFooterView:view];
    [_searchInforTableview setTableHeaderView:view];
    [self.view addSubview:_searchInforTableview];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)addfriendbtn:(id)sender
{
    FaceAddFriendViewController * faceaddfriend = [[FaceAddFriendViewController alloc] init];
    [kAppDelegate.iCSDrawerController.navigationController pushViewController:faceaddfriend animated:NO ];
    
}


- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    if (searchBar.text == nil) {
        [self showAlerHUD:@"请输入学工号或者姓名！"];
        return;
    }
    //20151010
    [searchBar resignFirstResponder];
    
    NSDictionary *map = @{@"keyword":searchBar.text};
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [DE serverCall:@"findUser" params:map callback:^(BOOL success, int code, NSString *desc, NSDictionary *data,NSDictionary*param) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if(success)
        {
            listArr = [data objectForKey:@"users"];
            [_searchInforTableview reloadData];
        }
        else{
            [self showAlerHUD:@"查找人失败"];
        }
    }];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    
    /*
    if (searchBar.text == nil) {
        [self showAlerHUD:@"请输入学工号或者姓名！"];
    }
    
    if(searchText.length>0)
    {
        NSDictionary *map = @{@"keyword":searchText};
        
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        [DE serverCall:@"findUser" params:map callback:^(BOOL success, int code, NSString *desc, NSDictionary *data,NSDictionary*param) {
            NSLog(@"VVVVVVVV  %d ",code);
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if(success)
            {
                listArr = [data objectForKey:@"users"];
                [_searchInforTableview reloadData];
            }
            else{
                [self showAlerHUD:@"查找人失败"];
            }
        }];
    }
    */
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return listArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"SearchinforTableViewCell";
    SearchinforTableViewCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
    }
    NSDictionary * dic =[listArr objectAtIndex:indexPath.row];
    NSString * str = [NSString stringWithFormat:@"%@(%@)",[dic objectForKey:@"userName" ],[[dic objectForKey:@"type"] isEqualToString:@"T"]?@"教职工":@"学生"];
    NSURL *url= [NSURL URLWithString: [[dic objectForKey:@"headImg"] hasPrefix:@"http"]?[dic objectForKey:@"headImg"]:[NSString stringWithFormat:@"%@%@",DE.fileEngineeUrl,[dic objectForKey:@"headImg"]]];
    cell.headImage.layer.masksToBounds = YES;
    cell.headImage.layer.cornerRadius = 8.0;
    [cell.headImage setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
    cell.nameLabel.text = str;
    //cell.detailLabel.text = [dic objectForKey:@"dept"];
    cell.detailLabel.text = [[NSString alloc]initWithFormat:@"%@/%@", [dic objectForKey:@"dept"],[dic objectForKey:@"profName"]];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后颜色消失
    FriendDetailViewController * frienddetail = [[FriendDetailViewController alloc] init];
    frienddetail.friendInfoDic =[listArr objectAtIndex:indexPath.row];
    [kAppDelegate.iCSDrawerController.navigationController pushViewController:frienddetail animated:NO];
}


@end
